<template>
	<view>
		<u-swipe-action :index="index" v-for="(item, index) in list" :key="item.id" @click="click"
			@open="open" :options="options">
			<view class="item u-border-bottom">
				<image mode="aspectFill" :src="item.img" />
				<!-- 此层wrap在此为必写的，否则可能会出现标题定位错误 -->
				<view class="title-wrap">
					<text class="rc-text" :style="{color:item.status=='未发布'?'#ff4949':'black'}" >{{ `${item.brand}  ${item.code}` }}</text>
					<view style="display: flex;margin-top: 28rpx;color: #acacac;">
						{{item.type}}
						<u-line color="red" direction='col' margin='10rpx'/>
						{{item.gear}}
						<u-line color="red" direction='col' margin='10rpx'/>
						{{item.seat+'座'}}
						<u-line color="red" direction='col' margin='10rpx'/>
						{{item.tank+'L'}}
						<u-line color="red" direction='col' margin='10rpx'/>
					</view>
				</view>
			</view>
		</u-swipe-action>
		<u-button @click="addCar" shape="circle" :custom-style="{width:'100%',marginTop:'40rpx'}" type="primary"
			style="width: 86%;">
			添加车辆</u-button>
	</view>
</template>

<script>
	import SET from '@/SET.js'
	export default {
		data() {
			return {
				baseUrl: SET.baseUrl,
				list: [],
				disabled: false,
				btnWidth: 180,
				show: false,
				options: [{
						text: '发布',
						style: {
							backgroundColor: '#bbca12'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			}
		},
		onShow() {
			let model = {}
			model.userId = this.$store.state.userInfo.id
			this.$api.getCar(model).then(res=>{
				this.list = res.result.records.map(item=>{
					item.img = this.baseUrl + item.imgs.split(',')[0]
					return item
				})
			})
		},
		methods: {
			addCar() {
				uni.navigateTo({
					url: '/pages/user/car/addCar'
				})
			},
			click(index, index1) {
				if (index1 == 1) {
					this.$api.delCar(this.list[index].id).then(res=>{
						if(res.result === '删除成功!'){
							this.list.splice(index, 1)
							this.$u.toast(`删除成功`)
						}
					})
					
				} else {
					this.list[index].show = false
					uni.navigateTo({
						url:'/pages/user/publish/myPublish'
					})
				}
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.list[index].show = true
				this.list.map((val, idx) => {
					if (index != idx) this.list[idx].show = false
				})
			}
		}
	}
</script>

<style>
	.rc-text{
		font-size: 32rpx;
		font-weight: bold;
	}
	
	.item {
		display: flex;
		padding: 20rpx;
	}

	image {
		width: 120rpx;
		flex: 0 0 120rpx;
		height: 120rpx;
		margin-right: 20rpx;
		border-radius: 12rpx;
	}

	.title {
		text-align: left;
		font-size: 28rpx;
		color: $u-content-color;
		margin-top: 20rpx;
	}
</style>
